<?php
/**
 * Created by yuxianjun001@icloud.com.
 * User: wuchen
 * Date: 2019/4/1
 * Time: 11:03
 */

namespace App\Services\Goods;

use App\Helpers\ArrayHelper;
use App\model\GoodsPromotionModel\GoodsPromotionModel;
use App\Services\BaseService;
use App\model\GoodsModel\BusinessGoodsModel;
use Illuminate\Support\Facades\DB;

class BusinessGoodsService extends BaseService
{

    protected $model;

    public function __construct()
    {
        $this->model = new BusinessGoodsModel();
    }

    /**
     * 商家商品列表
     * @param $request
     */
    public function list($request)
    {
        try {
            $dbResult = DB::table('business_goods as bg')->where('bg.deleted_at', null);
            $dbResult->leftJoin('plat_form_goods as pg', 'bg.goods_id', '=', 'pg.id')->select('pg.*', 'bg.status as goods_status');
            if (!empty($request->name)) {
                $dbResult = $dbResult->where('pg.name', 'like', '%' . $request->name . '%');
            }
            if (!empty($request->status)) {
                $dbResult = $dbResult->where('bg.status', $request->status);
            }


            $res = $this->model->buildPaginate($dbResult, $request);

            return $this->response($res, '查询成功');

        } catch (\Exception $e) {
            return $this->response($e->getMessage(), '查询失败', 500);
        }

    }

    /**
     * 查看商品详情
     * @param Request $request
     */
    public function info($request)
    {
        $res = DB::table('business_goods as bg')->leftJoin('plat_form_goods as pg', 'bg.goods_id', '=', 'pg.id')
            ->where('bg.id', $request->id)->select('bg.id as business_id ', 'bg.status as business_status', 'pg.*')->first();
        if (empty($res)) {
            return $this->response('', '商品不存在于商家商品列表', 404);
        }
        return $this->response(ArrayHelper::ObjectToArray($res), '查询成功');

    }

    /**
     * 商家商品上下架
     * @param Request $request
     */
    public function upAndDown($request)
    {
        try {
            $goods = BusinessGoodsModel::where(['id' => $request->id, 'deleted_at' => null])->first();
            if (empty($goods)) {
                return $this->response('', '商品不存在', 404);
            }

            $data = [
                'status' => $request->status ?: 0,//1-上架，0-下架
            ];
            $res  = $this->model->where('id', $request->id)->update($data);
            if (!empty($res)) {
                return $this->response('', '商品上下架成功');
            }

        } catch (\Exception $e) {
            return $this->response($e->getMessage(), '商品上下架异常', 500);
        }

    }

    /**
     * 申请促销
     * @param Request $request
     */
    public function promotion($request)
    {
        try {
            $model                = new GoodsPromotionModel();
            $model->business_id   = $request->businessId; //商户id
            $model->business_name = $request->businessName;
            $model->goods_id      = $request->goodsId;
            $model->goods_name    = $request->goodsName;
            $model->num           = $request->num;
            $model->status        = 0;//0-新申请，1-已经通过

            $res = $model->save();

            if (!empty($res)) {
                return $this->response('', '申请促销成功');

            } else {
                return $this->response('', '申请促销失败', 500);
            }

        } catch (\Exception $e) {
            return $this->response($e->getMessage(), '申请促销异常', 500);
        }

    }

    /**
     * 上下移动排序
     * @param Request $request
     */
    public function sort($request)
    {

    }

}